package com.hudongwx.my_shian.adapter.homeAdapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hudongwx.my_shian.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.x;

/**
 * Author:曾传标
 * time：16-12-13 下午6:42
 */
public class MyChosenAdapter extends BaseAdapter {

    JSONArray data;
    Context context;

    public MyChosenAdapter(JSONArray data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.length();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        ViewHolder viewHolder;
        if (null == convertView) {//没有可以复用的布局
            //解析布局
            convertView = LayoutInflater.from(context).inflate(R.layout.home_chosen_item, null);
            //实例化一个ViewHolder
            viewHolder = new ViewHolder();
            //找出需要显示的控件
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.home_chosen_list_item_iv);
            viewHolder.textView = (TextView) convertView.findViewById(R.id.home_chosen_list_item_tv_1);
            convertView.setTag(viewHolder);
        } else {
            //有可以复用的布局
            viewHolder = (ViewHolder) convertView.getTag();
        }

        //把数据设置给控件
        try {
            x.image().bind(viewHolder.imageView, data.getJSONObject(position).getString("CoverUrl"));
            Log.i("s", "getView: " + (viewHolder.textView == null));
            viewHolder.textView.setText(data.getJSONObject(position).getString("Title"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return convertView;
    }

    class ViewHolder {
        ImageView imageView;
        TextView textView;
    }


}
